package com.coder.baseapp.wrapper;

import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

import io.reactivex.Flowable;
import io.reactivex.processors.FlowableProcessor;
import io.reactivex.processors.PublishProcessor;
import io.reactivex.subjects.Subject;

/**
 * @Author: Tua.CoderGui
 * *
 * @Date: 2017/3/11
 * *
 * Copyright (c) 2017 All Rights Reserved.
 */
public class RxBus {

    private static volatile RxBus defaultInstance;

    //存放订阅者信息
    private FlowableProcessor<Object> mBus = null;

    private RxBus() {
        mBus = PublishProcessor.create().toSerialized();
    }

    public static synchronized RxBus getInstance() {
        if (null == defaultInstance) {
            defaultInstance = new RxBus();
        }
        return defaultInstance;
    }

    // 发送一个新的事件
    public synchronized void post(Object o) {
        mBus.onNext(o);
    }

    // 根据传递的 eventType 类型返回特定类型(eventType)的 被观察者
    public synchronized <T> Flowable<T> toFlowable(Class<T> eventType) {
        return mBus.ofType(eventType);
    }
}
